using System.Collections;
using Holoville.HOTween;
using UnityEngine;

public class LevelUpIndicator : MonoBehaviour
{
	public static LevelUpIndicator instance;

	public UILabel label;

	public GameObject lightning;

	public float animationTimeBeforeParticle = 2f;

	public float stayOnScreenTime = 2f;

	public float animationTimeAfterParticle = 2f;

	public Vector3 Scale;

	public bool IsAnimationPlay;

	[HideInInspector]
	public float _alpha;

	private void Awake()
	{
		instance = this;
	}

	private void Start()
	{
		HOTween.Init();
		Reset();
	}

	private void OnDestroy()
	{
		instance = null;
	}

	private void Update()
	{
		if (IsAnimationPlay)
		{
			label.alpha = _alpha;
		}
	}

	public void ActivateIndicator()
	{
		if ((MissionCompleted.instance != null && MissionCompleted.instance.IsAnimationPlay) || (IndicatorWinPvP.instance != null && IndicatorWinPvP.instance.IsAnimationPlay))
		{
			StartCoroutine(WaitAnimation());
		}
		else
		{
			StartAnimation();
		}
	}

	private IEnumerator WaitAnimation()
	{
		yield return new WaitForSeconds(1f);
		if (MissionCompleted.instance.IsAnimationPlay || IndicatorWinPvP.instance.IsAnimationPlay)
		{
			StartCoroutine(WaitAnimation());
		}
		else
		{
			StartAnimation();
		}
	}

	public void StartAnimation()
	{
		if (ExpHUD.instance != null)
		{
			ExpHUD.instance.SetLevelLabel(1);
		}
		if (!IsAnimationPlay)
		{
			IsAnimationPlay = true;
			label.gameObject.SetActive(true);
			label.transform.localScale = Vector3.one;
			HOTween.Kill(label.transform);
			HOTween.To(label.transform, animationTimeBeforeParticle, new TweenParms().Prop("localScale", Scale).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(DeactivateIndicator));
			HOTween.To(this, animationTimeBeforeParticle, new TweenParms().Prop("_alpha", 1f));
		}
	}

	public void DeactivateIndicator()
	{
		Invoke("Wait", stayOnScreenTime);
	}

	private void Wait()
	{
		HOTween.Kill(label.transform);
		HOTween.To(label.transform, animationTimeAfterParticle, new TweenParms().Prop("localScale", Vector3.one * 0.01f).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete(Reset));
		HOTween.To(this, animationTimeAfterParticle, new TweenParms().Prop("_alpha", 0f));
	}

	public void Reset()
	{
		IsAnimationPlay = false;
		StopAllCoroutines();
		_alpha = 0f;
		label.alpha = _alpha;
		label.transform.localScale = Vector3.one;
		lightning.SetActive(false);
		label.gameObject.SetActive(false);
	}
}
